package com.companyName.generator;

import org.apache.commons.logging.LogFactory;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

public class GenerateCodeListener implements ActionListener {

	private final static org.apache.commons.logging.Log log = LogFactory.getLog(GenerateCodeListener.class);

	private JTextField txtSampleDir;
	private JTextField txtTargetDir;
	private JTextField txtProjectName;
	private JTextField txtCompanyName;
	private JTextField txtDbUrl;
	private JTextField txtUserName;
	private JTextField txtPassword;
	private JTextField txtPluginsName;


	public GenerateCodeListener(){
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		CodeGenerator generator = new CodeGenerator();
		log.info("=============自动生成开始===============");

		generator.generate(false,txtSampleDir.getText(), txtTargetDir.getText(), txtProjectName.getText(),txtCompanyName.getText(),txtDbUrl.getText(),txtUserName.getText(),txtPassword.getText(),txtPluginsName.getText());
 		String pluginsName = txtPluginsName.getText();
		String sampleDir = txtSampleDir.getText();
		String targetDir = txtTargetDir.getText();
		//生成插件开始
		if(pluginsName.indexOf("cat")>=0) {
			log.info("=============生成cat插件开始===============");
			String sampleDirPlugins = sampleDir.replaceAll("\\\\fw-sample", "\\\\fw-generator\\\\plugins\\\\cat");

			generator.generate(true,sampleDirPlugins, targetDir, txtProjectName.getText(),txtCompanyName.getText(),txtDbUrl.getText(),txtUserName.getText(),txtPassword.getText(),txtPluginsName.getText());
			log.info("=============生成cat插件完成===============");
		}
		//生成插件开始
		if(pluginsName.indexOf("hessian")>=0) {
			log.info("=============生成hessian插件开始===============");
			String sampleDirPlugins = sampleDir.replaceAll("\\\\fw-sample", "\\\\fw-generator\\\\plugins\\\\hessian");

			generator.generate(true,sampleDirPlugins, targetDir, txtProjectName.getText(),txtCompanyName.getText(),txtDbUrl.getText(),txtUserName.getText(),txtPassword.getText(),txtPluginsName.getText());
			log.info("=============生成hessian插件完成===============");
		}
		if(pluginsName.indexOf("dubbo")>=0) {
			log.info("=============生成dubbo插件开始===============");
			String sampleDirPlugins = sampleDir.replaceAll("\\\\fw-sample", "\\\\fw-generator\\\\plugins\\\\dubbo");

			generator.generate(true,sampleDirPlugins, targetDir, txtProjectName.getText(),txtCompanyName.getText(),txtDbUrl.getText(),txtUserName.getText(),txtPassword.getText(),txtPluginsName.getText());
			log.info("=============生成dubbo插件完成===============");
		}
		if(pluginsName.indexOf("rabbitmq")>= 0) {
			log.info("=============生成rabbitmq插件开始===============");
			String sampleDirPlugins = sampleDir.replaceAll("\\\\fw-sample", "\\\\fw-generator\\\\plugins\\\\rabbitmq");

			generator.generate(true,sampleDirPlugins, targetDir, txtProjectName.getText(),txtCompanyName.getText(),txtDbUrl.getText(),txtUserName.getText(),txtPassword.getText(),txtPluginsName.getText());
			log.info("=============生成dubbo插件完成===============");
		}

		log.info("=============自动生成完成===============");
		if("".equals(txtPluginsName.getText())){
			log.info("*************此架构包不包含插件*************");
		}else{
			log.info("*************此架构包包含如下插件["+txtPluginsName.getText()+"]*************");
		}
	}

	public JTextField getTxtSampleDir() {
		return txtSampleDir;
	}

	public void setTxtSampleDir(JTextField txtSampleDir) {
		this.txtSampleDir = txtSampleDir;
	}

	public JTextField getTxtTargetDir() {
		return txtTargetDir;
	}

	public void setTxtTargetDir(JTextField txtTargetDir) {
		this.txtTargetDir = txtTargetDir;
	}

	public JTextField getTxtProjectName() {
		return txtProjectName;
	}

	public void setTxtProjectName(JTextField txtProjectName) {
		this.txtProjectName = txtProjectName;
	}

	public JTextField getTxtCompanyName() {
		return txtCompanyName;
	}

	public void setTxtCompanyName(JTextField txtCompanyName) {
		this.txtCompanyName = txtCompanyName;
	}

	public JTextField getTxtDbUrl() {
		return txtDbUrl;
	}

	public void setTxtDbUrl(JTextField txtDbUrl) {
		this.txtDbUrl = txtDbUrl;
	}

	public JTextField getTxtUserName() {
		return txtUserName;
	}

	public void setTxtUserName(JTextField txtUserName) {
		this.txtUserName = txtUserName;
	}

	public JTextField getTxtPassword() {
		return txtPassword;
	}

	public void setTxtPassword(JTextField txtPassword) {
		this.txtPassword = txtPassword;
	}

	public JTextField getTxtPluginsName() {
		return txtPluginsName;
	}

	public void setTxtPluginsName(JTextField txtPluginsName) {
		this.txtPluginsName = txtPluginsName;
	}


}
